//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/util/view_util.dart';

/// 购买页面卡片背景的相应样式
class PackageBackDecoration extends Decoration {
  /// 边框颜色起始
  final Color borderColorBegin;

  /// 边框颜色结束
  final Color borderColorEnd;

  /// 颜色起始
  final Color colorBegin;

  /// 颜色结束
  final Color colorEnd;

  const PackageBackDecoration(
      {required this.borderColorBegin,
        required this.borderColorEnd,
        required this.colorBegin,
        required this.colorEnd});

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _PackageBackPainter(
        this, borderColorBegin, borderColorEnd, colorBegin, colorEnd);
  }
}

class _PackageBackPainter extends BoxPainter {
  final PackageBackDecoration packageBackDecoration;

  /// 边框颜色起始
  final Color borderColorBegin;

  /// 边框颜色结束
  final Color borderColorEnd;

  /// 颜色起始
  final Color colorBegin;

  /// 颜色结束
  final Color colorEnd;

  const _PackageBackPainter(this.packageBackDecoration, this.borderColorBegin,
      this.borderColorEnd, this.colorBegin, this.colorEnd);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    var size = configuration.size;

    double leftX = offset.dx;
    double topY = offset.dy;
    double rightX = leftX + size!.width;
    double bottomY = topY + size.height;

    // 获取边框渐变路径
    Path path = _backPath(leftX, topY, rightX, bottomY);

    Paint paint = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 1
      ..shader = LinearGradient(
          colors: [borderColorBegin, borderColorEnd],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight)
          .createShader(path.getBounds());

    canvas.drawPath(path, paint);

    // 获取内容渐变路径
    Path path1 = _backPath(leftX, topY, rightX, bottomY, borderPx: 6);

    Paint paint1 = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 1
      ..shader = LinearGradient(
          colors: [colorBegin, colorEnd],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight)
          .createShader(path1.getBounds());

    canvas.drawPath(path1, paint1);

    // 阴影
    Path path2 = Path();
    path2.moveTo(leftX - 1, bottomY);
    path2.lineTo(leftX + 23, topY + 9);
    path2.close();

    BoxShadow shadow = BoxShadow(
      color: colorBlack3,
      offset: Offset(-2, 2),
      blurRadius: 8,
      spreadRadius: 0,
    );

    final Paint shadowPainter = shadow.toPaint();
    shadowPainter.style = PaintingStyle.stroke;
    if (shadow.spreadRadius == 0) {
      canvas.drawPath(path2.shift(shadow.offset), shadowPainter);
    } else {
      Rect zone = path2.getBounds();
      double xScale = (zone.width + shadow.spreadRadius) / zone.width;
      double yScale = (zone.height + shadow.spreadRadius) / zone.height;
      Matrix4 m4 = Matrix4.identity();
      m4.translate(zone.width / 2, zone.height / 2);
      m4.scale(xScale, yScale);
      m4.translate(-zone.width / 2, -zone.height / 2);
      canvas.drawPath(
          path2.shift(shadow.offset).transform(m4.storage), shadowPainter);
    }
  }

  /// 获取背景样式的路径
  Path _backPath(double leftX, double topY, double rightX, double bottomY,
      {double borderPx = 0}) {
    Path path = Path();
    path.moveTo(leftX, bottomY);
    path.lineTo(leftX + borderPx + 24, topY + borderPx + 9);
    path.quadraticBezierTo(leftX + borderPx + 26, topY + borderPx + 6,
        leftX + borderPx + 22, topY + borderPx);
    path.lineTo(rightX - borderPx - 24, topY + borderPx);
    path.quadraticBezierTo(rightX - borderPx, topY + borderPx,
        rightX - borderPx, topY + borderPx + 24);
    path.lineTo(rightX, bottomY - 24);
    path.quadraticBezierTo(rightX, bottomY, rightX - 24, bottomY);
    path.close();

    return path;
  }
}
